package com.HMI;

import java.awt.Canvas;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import com.Resources.CustomColor;

@SuppressWarnings("serial")
public class MainMenu extends JPanel {

	private Window parent;
	
	private MenuButton btnMain;
	private MenuButton btnChlorine;
	private MenuButton btnPh;
	private MenuButton btnSchedule;
	
	private ActionListener mainClick;
	private ActionListener chlorineClick;
	
	private JLabel lblDate;
	
	public MainMenu(Window parent) {
		this.parent = parent;
		initialize();
	}

	private void initialize() {
		this.setBackground(CustomColor.BLUE);
		this.setBounds(0, 0, 218, 480);
		parent.frmAutomatedPoolBalancer.getContentPane().add(this);
		this.setLayout(null);

		btnMain = new MenuButton("Main", 15, 112, 188, 39);
		btnChlorine = new MenuButton("Chlorine", 15, 152, 188, 39);
		btnPh = new MenuButton("pH", 15, 192, 188, 39);
		btnSchedule = new MenuButton("Schedule", 15, 232, 188, 39);
		
		highlightCurrentPage(btnMain, btnChlorine, btnPh, btnSchedule);
		
		mainClick = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				parent.setWindow(parent.MAIN);
				highlightCurrentPage(btnMain, btnChlorine, btnPh, btnSchedule);
			}
		};
		
		btnMain.addActionListener(mainClick);
		this.add(btnMain);
	
		chlorineClick = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				parent.setWindow(parent.CHLORINE);
				highlightCurrentPage(btnChlorine, btnMain, btnPh, btnSchedule);
			}
		};
		
		btnChlorine.addActionListener(chlorineClick);
		this.add(btnChlorine);
		
		btnPh.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				parent.setWindow(parent.PH);
				highlightCurrentPage(btnPh, btnMain, btnChlorine, btnSchedule);
			}
		});
		this.add(btnPh);
		
		btnSchedule.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				parent.setWindow(parent.SCHEDULE);
				highlightCurrentPage(btnSchedule, btnMain, btnChlorine, btnPh);
			}
		});
		this.add(btnSchedule);
		
		SetMenuBreaks();

		String date = new SimpleDateFormat("EEE, MMM d").format(Calendar.getInstance().getTime());
		
		lblDate = new JLabel(date);
		lblDate.setFont(new Font("DejaVu Sans", Font.PLAIN, 20));
		lblDate.setBounds(33, 425, 180, 21);
		lblDate.setHorizontalAlignment(SwingConstants.LEFT);
		lblDate.setForeground(CustomColor.WHITE_BLUE);
		this.add(lblDate);
		
		JLabel lblClock = new JLabel("");
		lblClock.setFont(new Font("DejaVu Sans", Font.PLAIN, 28));
		lblClock.setBounds(33, 389, 180, 30);
		lblClock.setHorizontalAlignment(SwingConstants.LEFT);
		lblClock.setForeground(CustomColor.WHITE_BLUE);
		this.add(lblClock);
		new Clock(lblClock, date, this);
		
	}
	
	private void SetMenuBreaks() {
		Canvas menuBreak1 = new Canvas();
		MenuBreak(menuBreak1, 15, 111, 188, 1);	
		Canvas menuBreak2 = new Canvas();
		MenuBreak(menuBreak2, 15, 151, 188, 1);
		Canvas menuBreak3 = new Canvas();
		MenuBreak(menuBreak3, 15, 191, 188, 1);
		Canvas menuBreak4 = new Canvas();
		MenuBreak(menuBreak4, 15, 231, 188, 1);
	}
	
	private void MenuBreak(Canvas canvas, int x, int y, int width, int height) {
		canvas.setBounds(x, y, width, height);
		canvas.setBackground(CustomColor.GREY_BLUE);
		this.add(canvas);
	}
	
	private void highlightCurrentPage(MenuButton selected, MenuButton other1,
			MenuButton other2, MenuButton other3) {
		selected.setOpaque(true);
		selected.setBackground(CustomColor.NEAR_WHITE);
		selected.setForeground(CustomColor.NAVY_BLUE);
		
		other1.setOpaque(false);
		other1.setForeground(CustomColor.WHITE);
		other2.setOpaque(false);
		other2.setForeground(CustomColor.WHITE);
		other3.setOpaque(false);
		other3.setForeground(CustomColor.WHITE);
	}
	
	public void refreshPopop() {
		parent.refreshPopup();
	}
	
	public void updateDate(String date) {
		lblDate.setText(date);
		parent.updateScheduleDates();
	}
	
	public void updateGraphAxes() {
		parent.updateGraphAxes();
	}
}
